package com.singi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eova.common.base.BaseController;
import com.eova.common.utils.util.StringUtils;
import com.jfinal.aop.Clear;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;

/**
 * Created by Administrator on 2020-5-27.
 */
@Clear
public class SearchController  extends BaseController {
    private String domain_url="https://www.xinjisx.com";
    //搜索查询
    public void getStoreBySearch(@Para("search_str") String search_str, @Para("language") String language, @Para("page") int page){
        int page_num = page - 1;
        List<Record> storeList;
        if(language.equalsIgnoreCase("cn")){
            storeList =  Db.find("SELECT\n" +
                    "\tid,\n" +
                    "\tmarket_type,\n" +
                    "\tcategory,\n" +
                    "\tmainlabel,\n" +
                    "\tstore_name,\n" +
                    "\tstore_no,\n" +
                    "\tlogo,\n" +
                    "\tphone,\n" +
                    "\tproduct_show_pic\n" +
                    "FROM\n" +
                    "\txj_store\n" +
                    "WHERE\n" +
                    "\tstore_name LIKE '%"+search_str+"%' \n" +
                    "\torder by is_up desc,id asc \n" +
                    "\tlimit ?,10",page_num);

        }else{
            storeList =  Db.find("SELECT\n" +
                    "\tid,\n" +
                    "\tmarket_type,\n" +
                    "\tcategory,\n" +
                    "\tmainlabel_en as mainlabel,\n" +
                    "\tstore_name_en as store_name,\n" +
                    "\tstore_no,\n" +
                    "\tlogo,\n" +
                    "\tproduct_show_pic\n" +
                    "FROM\n" +
                    "\txj_store\n" +
                    "WHERE\n" +
                    "\tstore_name_en LIKE '%"+search_str+"%' \n"+
                    "\torder by is_up desc,id asc \n" +
                    "\tlimit ?,10",page_num);

        }

        for(int i = 0; i<storeList.size();i++){
            storeList.get(i).set("logo",domain_url+"/store/logo/"+storeList.get(i).get("logo"));
            //产品图
            if(!StringUtils.isEmpty(storeList.get(i).get("product_show_pic"))){
                String[] productList = storeList.get(i).get("product_show_pic").toString().split(",");
                JSONArray productArray = new JSONArray();
                for(int j=0;j<productList.length; j++){
                    JSONObject productOject = new JSONObject();
                    String product_str = domain_url+"/store/product/"+productList[j];
                    productOject.put("url",product_str);
                    productArray.add(productOject);
                }
                storeList.get(i).set("product_show_pic", productArray);
            }

        }

        renderJson(storeList);
    }


    //品类搜索查询
    public void getStoreCategoryBySearch(@Para("search_str") String search_str,@Para("language") String language,@Para("page") int page){
        int page_num = page - 1;
        List<Record> storeList;
        List<Record> storeListArray;
        List<Record> categorySearchList;
        String sql="";


        if(language.equalsIgnoreCase("cn")){

            categorySearchList = Db.find("SELECT\n" +
                    "\tid\n" +
                    "FROM\n" +
                    "\txj_enum_item\n" +
                    "WHERE\n" +
                    "\tenum_value LIKE '%"+search_str+"%'");


            sql = "SELECT DISTINCT t.id,t.market_type,t.category,t.mainlabel,t.store_name,t.store_no,t.logo,t.phone,t.product_show_pic from (";


            for (int a = 0;a<categorySearchList.size();a++){

                if(a == 0){
                    sql += "SELECT\n" +
                            "\tid,\n" +
                            "\tmarket_type,\n" +
                            "\tcategory,\n" +
                            "\tmainlabel,\n" +
                            "\tstore_name,\n" +
                            "\tstore_no,\n" +
                            "\tlogo,\n" +
                            "\tphone,\n" +
                            "\tproduct_show_pic\n" +
                            "FROM\n" +
                            "\txj_store\n" +
                            "WHERE\n" +
                            "\tcategory LIKE '%"+categorySearchList.get(a).get("id")+"%'";
                }else{
                    sql += " union ALL ";
                    sql += "SELECT\n" +
                            "\tid,\n" +
                            "\tmarket_type,\n" +
                            "\tcategory,\n" +
                            "\tmainlabel,\n" +
                            "\tstore_name,\n" +
                            "\tstore_no,\n" +
                            "\tlogo,\n" +
                            "\tphone,\n" +
                            "\tproduct_show_pic\n" +
                            "FROM\n" +
                            "\txj_store\n" +
                            "WHERE\n" +
                            "\tcategory LIKE '%"+categorySearchList.get(a).get("id")+"%'";
                }



            }
            sql += ") t";

            sql +="\torder by id asc \n";
            sql +="\tlimit ?,10";

            System.err.println(sql);

            storeList =  Db.find(sql,page_num);

            for(int i = 0; i<storeList.size();i++){
                //主营
                String[] categoryList = storeList.get(i).get("category").toString().split(",");
                //JSONObject categoryArray = new JSONObject();
                String categoryArray = "";
                for(int j=0;j<categoryList.length; j++){
                    String category_str = Db.queryStr("select enum_value from xj_enum_item where id = ?",categoryList[j]);
                    categoryArray += category_str+",";
                    if(j == categoryList.length-1){
                        categoryArray = categoryArray.substring(0,categoryArray.length()-1);
                    }
                }
                storeList.get(i).set("category",categoryArray);
            }

        }else{
            categorySearchList = Db.find("SELECT\n" +
                    "\tid\n" +
                    "FROM\n" +
                    "\txj_enum_item\n" +
                    "WHERE\n" +
                    "\tvcode LIKE '%"+search_str+"%'");

            sql = "SELECT DISTINCT t.id,t.market_type,t.category,t.mainlabel_en as mainlabel ,t.store_name_en as store_name,t.store_no,t.logo,t.phone,t.product_show_pic from (";
            for (int a = 0;a<categorySearchList.size();a++){

                if(a == 0){
                    sql += "SELECT\n" +
                            "\tid,\n" +
                            "\tmarket_type,\n" +
                            "\tcategory,\n" +
                            "\tmainlabel_en as mainlabel,\n" +
                            "\tstore_name_en as store_name,\n" +
                            "\tstore_no,\n" +
                            "\tlogo,\n" +
                            "\tphone,\n" +
                            "\tproduct_show_pic\n" +
                            "FROM\n" +
                            "\txj_store\n" +
                            "WHERE\n" +
                            "\tcategory LIKE '%"+categorySearchList.get(a).get("id")+"%'";
                }else{
                    sql += " union ALL ";
                    sql += "SELECT\n" +
                            "\tid,\n" +
                            "\tmarket_type,\n" +
                            "\tcategory,\n" +
                            "\tmainlabel_en as mainlabel,\n" +
                            "\tstore_name_en as store_name,\n" +
                            "\tstore_no,\n" +
                            "\tlogo,\n" +
                            "\tphone,\n" +
                            "\tproduct_show_pic\n" +
                            "FROM\n" +
                            "\txj_store\n" +
                            "WHERE\n" +
                            "\tcategory LIKE '%"+categorySearchList.get(a).get("id")+"%'";
                }



            }
            sql += ") t";


            storeList =  Db.find(sql);


        }

        for(int i = 0; i<storeList.size();i++){
            storeList.get(i).set("logo",domain_url+"/store/logo/"+storeList.get(i).get("logo"));
            //产品图
            if(!StringUtils.isEmpty( storeList.get(i).get("product_show_pic"))){
                String[] productList = storeList.get(i).get("product_show_pic").toString().split(",");
                JSONArray productArray = new JSONArray();
                for(int j=0;j<productList.length; j++){
                    JSONObject productOject = new JSONObject();
                    String product_str = domain_url+"/store/product/"+productList[j];
                    productOject.put("url",product_str);
                    productArray.add(productOject);
                }
                storeList.get(i).set("product_show_pic", productArray);
            }

        }

        renderJson(storeList);

    }
}
